package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.advanced;

import io.netty.channel.Channel;
import io.netty.handler.codec.socksx.v5.DefaultSocks5CommandResponse;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5CommandStatus;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.socks.handler.NettySocksClientProxyClientVisitorInboundHandler;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.client.AbstractHandleSocksDistributeClientProxyClientConnectionTransferSuccessTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.protocol.handler.NettySocks5CommandRequestHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;

@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
public class ClientHandleDistributeSocksClientProxyClientConnectionTransferSuccessfulAdvanced extends
        AbstractHandleSocksDistributeClientProxyClientConnectionTransferSuccessTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

        // 传输通道
        Channel transferChannel = nettyChannelContext.channel();

        // 根据传输通道获取代理通道
        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(transferChannel);

        Byte socks5AddressTypeByte = ChannelAttributeKeyUtils.getSocks5AddressType(nextChannel);
        Socks5AddressType socks5AddressType = Socks5AddressType.valueOf(socks5AddressTypeByte);
        //添加客户端转发请求到服务端的Handler
        // TODO bug fix
        nextChannel.pipeline().addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));
        // 请求数据开始上报
        nextChannel.pipeline().addLast(new NettySocksClientProxyClientVisitorInboundHandler());
        DefaultSocks5CommandResponse commandResponse =
                new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, socks5AddressType);
        nextChannel.writeAndFlush(commandResponse);
        nextChannel.pipeline().remove(NettySocks5CommandRequestHandler.class);
        nextChannel.pipeline().remove(Socks5CommandRequestDecoder.class);
    }
}
